package com.my_orm.datasource;

import com.my_orm.util.Dom4jUtil;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.dom4j.Document;

import java.io.UnsupportedEncodingException;
import java.util.Map;

/**
 * description: 用于解析连接部分配置文件
 * @author 12103527826@qq.com
 */
@Slf4j
@Getter
public class DataSourceConfig {

    /**
     * 核心配置文件名称
     */

    private static String configName = "orm.cfg.xml";

    /**
     * classpath 路径
     */
    private static String classpath;

    /**
     * 数据库源信息
     */
    private static Map<String,String> sourceProp;


    public static  Map<String,String> getSourceProp() {
        return sourceProp;
    }


    static {
        //classpath 路径
        classpath = new Thread().getContextClassLoader().getResource("").getPath();
        System.out.println("1111111111");
        System.out.println(classpath);
        if (classpath != null) {
            try {
                classpath = java.net.URLDecoder.decode(classpath, "utf-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
                log.error("get classpath error ,{}",e);
            }

        }
        Document document = Dom4jUtil.getXMLByFilePath(classpath + configName);
        sourceProp = Dom4jUtil.Elements2Map(document, "property", "name");
    }
}


